/*******************************************************************************
 * Copyright 2011 Towee.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package net.towee.server;

import net.towee.model.discussion.DiscussionFactory;
import net.towee.model.repo.AttachmentFactory;
import net.towee.model.repo.DocumentFactory;
import net.towee.server.authentication.EmailSender;
import net.towee.server.authentication.SessionAuthManager;
import net.towee.server.authentication.impl.EmailSenderImpl;
import net.towee.server.authentication.impl.SessionAuthManagerImpl;
import net.towee.server.authentication.impl.SessionHttpManagerImpl;
import net.towee.server.bittorrent.AnnounceService;
import net.towee.server.bittorrent.AnnounceUpdater;
import net.towee.server.bittorrent.TorrentFactory;
import net.towee.server.bittorrent.impl.AnnounceServiceServlet;
import net.towee.server.bittorrent.impl.AnnounceUpdatedImpl;
import net.towee.server.factories.AttachmentFactoryImpl;
import net.towee.server.factories.DiscussionFactoryImpl;
import net.towee.server.factories.DocumentFactoryImpl;
import net.towee.server.factories.TorrentFactoryImpl;
import net.towee.server.ftp.CommandFactoryProvider;
import net.towee.server.ftp.FtpUserManager;
import net.towee.server.ftp.ToweeFileSystemFactory;
import net.towee.server.rpc.services.ToweeService;
import net.towee.server.rpc.servlet.ToweeServiceServlet;
import net.towee.server.xmpp.ToweeXMPPStarter;
import net.towee.server.xmpp.XmppAccountManager;
import net.towee.server.xmpp.XmppRosterManager;

import org.apache.ftpserver.command.CommandFactory;
import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.vysper.xmpp.authentication.AccountManagement;
import org.apache.vysper.xmpp.modules.roster.persistence.RosterManager;
import org.apache.vysper.xmpp.server.ServerRuntimeContext;
import org.eclipse.jetty.server.SessionManager;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.mycila.event.Dispatcher;

/**
 * Guice Module for the prototype Server.
 */
public class ServerModule extends AbstractModule {

	@Override
	protected void configure() {
		// Session managers
		bind(SessionAuthManager.class).to(SessionAuthManagerImpl.class).in(Singleton.class);
		bind(SessionManager.class).to(SessionHttpManagerImpl.class).in(Singleton.class);
		bind(EmailSender.class).to(EmailSenderImpl.class).in(Singleton.class);
		
		// Factories
		bind(AttachmentFactory.class).to(AttachmentFactoryImpl.class).in(Singleton.class);
		bind(DiscussionFactory.class).to(DiscussionFactoryImpl.class).in(Singleton.class);
		bind(TorrentFactory.class).to(TorrentFactoryImpl.class).in(Singleton.class);
		bind(DocumentFactory.class).to(DocumentFactoryImpl.class).in(Singleton.class);
		
		// BitTorrent
		bind(AnnounceService.class).to(AnnounceServiceServlet.class).in(Singleton.class);
		bind(AnnounceUpdater.class).to(AnnounceUpdatedImpl.class).in(Singleton.class);
		
		// FTP
		bind(UserManager.class).to(FtpUserManager.class);
		bind(FileSystemFactory.class).to(ToweeFileSystemFactory.class);
		bind(CommandFactory.class).toProvider(CommandFactoryProvider.class);
		
		// Servlets
		bind(ServerRpcProvider.class).in(Singleton.class);
		bind(ToweeService.class).to(ToweeServiceServlet.class).in(Singleton.class);
		bind(Dispatcher.class).toProvider(DispatcherProvider.class).in(Singleton.class);
		
		// XMPP
		bind(RosterManager.class).to(XmppRosterManager.class);
		bind(AccountManagement.class).to(XmppAccountManager.class);
		bind(ServerRuntimeContext.class).toProvider(ToweeXMPPStarter.class).in(Singleton.class);
	}
}
